package com.zlt.service.impl;

import com.zlt.annotation.Transactional;
import com.zlt.dao.Impl.ResumeDaoImpl;
import com.zlt.dao.ResumeDao;
import com.zlt.dto.OnlineResumeDTO;
import com.zlt.entity.*;
import com.zlt.service.ResumeService;
import com.zlt.util.ActiveUserUtil;
import com.zlt.util.ClassUtil;
import com.zlt.util.StringUtil;
import com.zlt.vo.ActiveUser;
import com.zlt.vo.OnlineResumeVO;
import com.zlt.vo.ResumeVO;
import com.zlt.vo.UserVo;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class ResumeServiceImpl implements ResumeService {

    private ResumeDao resumeDao = new ResumeDaoImpl();

    @Override
    @Transactional
    public boolean createOnlineResume(OnlineResumeDTO onlineResumeDTO) {
        OnlineResume onlineResume = new OnlineResume();
        try {
            BeanUtils.copyProperties(onlineResume,onlineResumeDTO);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        User user = new User();
        try {
            BeanUtils.copyProperties(user,activeUser);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        onlineResume.setUser(user);
        return resumeDao.insertOnlineResume(onlineResume) > 0;
    }

    @Override
    public boolean updateAdvantage(String advantage) {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        String uid = activeUser.getUid();
        return resumeDao.updateAdvantageByUid(advantage , uid) > 0;
    }

    @Override
    public boolean updateExpectedPosition(String expectedPosition) {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        String uid = activeUser.getUid();
        return resumeDao.updateExpectedPositionByUid(expectedPosition , uid) > 0;
    }

    @Override
    public boolean updateEducation(String education) {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        String uid = activeUser.getUid();
        return resumeDao.updateEducationByUid(education , uid) > 0;
    }

    @Override
    public boolean updateInternship(String internship) {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        String uid = activeUser.getUid();
        return resumeDao.updateInternshipByUid(internship , uid) > 0;
    }

    @Override
    public boolean updateSalary(String minSalary, String maxSalary) {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        String uid = activeUser.getUid();
        return resumeDao.updateSalaryByUid(minSalary, maxSalary , uid) > 0;
    }

    @Override
    public OnlineResumeVO queryOnlineResume() {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        String uid = activeUser.getUid();
        OnlineResume onlineResume = resumeDao.selectOnlineResumeByUid(uid);
        OnlineResumeVO onlineResumeVO = new OnlineResumeVO();
        try {
            BeanUtils.copyProperties(onlineResumeVO,onlineResume);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return onlineResumeVO;
    }

    @Override
    public List<ResumeVO> queryAllResume() {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        String uid = activeUser.getUid();
        List<Resume> resumeList = resumeDao.selectAllResumeByUid(uid);
        List<ResumeVO> resumeVOList = new ArrayList<>();
        resumeList.forEach(resume -> {
            ResumeVO resumeVO = new ResumeVO();
            try {
                BeanUtils.copyProperties(resumeVO, resume);
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
            resumeVOList.add(resumeVO);
        });
        return resumeVOList;
    }

    @Override
    public boolean deleteResume(Integer rid) {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        return resumeDao.deleteResumeByRid(rid) > 0;
    }

    @Override
    public ResumeVO previewResume(Integer rid) {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        Resume resume = resumeDao.selectResumePathByRid(rid);
        ResumeVO resumeVO = new ResumeVO();
        resumeVO.setPath(resume.getPath());
        return resumeVO;
    }

    @Override
    public boolean setupDefaultResumeToNormal() {
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        return resumeDao.updateDefaultResumeStateToNormal() >= 0;
    }

    @Override
    public boolean setupDefaultResume(Integer rid) {
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        return resumeDao.updateDefaultResumeByRid(rid) > 0;
    }

    @Override
    public boolean saveResume(String submittedFileName, String url) {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        String uid = activeUser.getUid();
        User user = new User();
        user.setUid(uid);

        Resume resume = new Resume();
        resume.setUser(user);
        resume.setFilename(submittedFileName);
        resume.setPath(url);
        resume.setCreateTime(StringUtil.getCurrentTime());
        resume.setState(0);
        return resumeDao.insertResume(resume) > 0;
    }

    @Override
    public UserVo getUSerByUid() {
        UserVo userVo = new UserVo();
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        String uid = activeUser.getUid();
        User user = resumeDao.getUSerByUid(uid);
        System.out.println(user);
        try {
            BeanUtils.copyProperties(userVo,user);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return userVo;
    }

    @Override
    public boolean updatePhoto(String url) {
        // TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        String uid = activeUser.getUid();
        return resumeDao.updatePhoto(uid,url)>0;
    }

    @Override
    public boolean sendResume(String cid,String jid) {
        Resume resume = resumeDao.selectDefaultResume();
        if (resume==null){
            new RuntimeException("你还没有设置默认简历！");
        }
        Delivery delivery = new Delivery();
        delivery.setResume(resume);
        //TODO 获取当前用户
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            new RuntimeException("未登录");
        }
        User user = new User();
        try {
            BeanUtils.copyProperties(user,activeUser);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        delivery.setUser(user);

        Company company = new Company();
        company.setCid(Integer.parseInt(cid));
        delivery.setCompany(company);

        Job job = new Job();
        job.setJid(Integer.parseInt(jid));
        delivery.setJob(job);
        delivery.setState(0);

        return resumeDao.insertDelivery(delivery)>0;
    }

}
